<?php
// $Id $

/**
 * Page callback function to load an activity stream page
 *
 * @param array $access_type
 * @return string Themed list of messages
 */
function heartbeat_messages_page($access_type, $offset_time = 0) {

  $output = '';

  if ($offset_time == 0) {
    $offset_time = $_SERVER['REQUEST_TIME'];
  }

  // Messages have to loaded by ajax if the url contains an offset
  // variable AND a post variable ajax is defined.
  $ajax = isset($_POST['ajax']) && $_POST['ajax'];
  $page = !isset($_POST['block']) || !$_POST['block'];

  // Normal page request with a offset time need a previous link
  if (!$ajax && $offset_time > 0 && $offset_time != $_SERVER['REQUEST_TIME']) {
    $output .= heartbeat_stream_prev_link($access_type);
  }

  // Message streams for each access type
  $context = heartbeat_stream_view($access_type, $page, $offset_time, $ajax);

  if (!isset($context)) {
    $data = t('No messages found.');
    return $ajax ? drupal_json(array('status' => TRUE, 'data' => $data)) : $data;
  }

  // Get the messages
  $messages = $context->execute();

  $heartbeatAccess = $context->getState();
  $link = '';
  if ($context->hasMoreMessages()) {
    $last_message = end($messages);
    $link = heartbeat_stream_more_link($heartbeatAccess, $last_message->timestamp, $page);
  }
  $theme_function = $ajax ? 'heartbeat_messages' : 'heartbeat_list';
  $output .= theme($theme_function, $messages, $heartbeatAccess, $link);

  if ($ajax) {
    return drupal_json(array('status' => TRUE, 'data' => $output));
  }

  return $output;
}

/**
 * Callback to poll for newer messages
 */
function heartbeat_activity_poll() {

  $latest_uaid = $_POST['latestUaid'];
  $access_type = $_POST['stream'];

  $context = heartbeat_stream_view($access_type, TRUE, 0, TRUE);

  if (!isset($context)) {
    return drupal_json(array('status' => TRUE, 'data' => ''));
  }

  $heartbeatAccess = $context->getState();
  $heartbeatAccess->stream->latest_uaid = $latest_uaid;

  $context->updateState($heartbeatAccess);

  // Get the messages
  $messages = $context->execute();

  $output = '';
  $output .= theme('heartbeat_messages', $messages, $context->getState());

  // Get the times to update to the current time
  $uaids = explode(',', $_POST['uaids']);
  $time_updates = array();
  $result = db_query("SELECT uaid, timestamp FROM {heartbeat_activity} WHERE uaid IN( %s ) ", implode(',', $uaids) );
  while ($row = db_fetch_object($result)) {
    $time_updates[$row->uaid] = _theme_time_ago($row->timestamp);
  }

  return drupal_json(array('status' => TRUE, 'data' => $output, 'time_updates' => $time_updates));
}

/**
 * Menu callback: confirm deleting of logs.
 */
function heartbeat_delete_log_confirm($form_state, $uaid) {
  $form = array(
    'uaid' => array(
      '#type' => 'hidden',
      '#value' => $uaid
    ),
    'redirect_path' => array(
      '#type' => 'hidden',
      '#value' => isset($_GET['destination']) ? $_GET['destination'] : $_SERVER['HTTP_REFERER']
    ),
  );
  return confirm_form($form,
    t('Are you sure you want to delete this message?'),
    $_GET['destination'],
    t('This action can not be undone.'),
    t('Delete'),
    t('Cancel')
  );
}

/**
 * Handler for wipe confirmation
 */
function heartbeat_delete_log_confirm_submit($form, &$form_state) {
  db_query("DELETE FROM {heartbeat_activity} WHERE uaid = %d", $form_state['values']['uaid']);
  $form_state['redirect'] = isset($_GET['destination']) ? $_GET['destination'] : $form_state['values']['redirect_path'];
  drupal_set_message('Message deleted');
}
